package com.ada.base.security;

import com.ada.base.security.entity.User;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserInitialization implements CommandLineRunner {
    private PasswordEncoder passwordEncoder;
    private JdbcTemplate jdbcTemplate;

    public UserInitialization(PasswordEncoder passwordEncoder, JdbcTemplate jdbcTemplate) {
        this.passwordEncoder = passwordEncoder;
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    public void run(String... args) {
        System.out.println("启动任务预留");
        //暂时不需要
//        String sql = "select username,password from user where username=?";
//        List<User> userList = jdbcTemplate.query(sql, new Object[]{"boss"}, new RowMapperResultSetExtractor<>(new BeanPropertyRowMapper<>(User.class), 1));
//        if (CollectionUtils.isEmpty(userList)) {
//            String createUser = "insert into user(username, password, staffNo, reallyName,workType) values(?,?,?,?,?)";
//            //TODO random password
//            jdbcTemplate.update(createUser, "boss", passwordEncoder.encode("654321"), "88888888", "总裁账号", "总裁");
//        }
    }


    private static long ada;

    static {
        ada = 500L;
    }

    public static void setAda(long ada) {
        UserInitialization.ada = ada;
    }

    public static long getAda() {
        return UserInitialization.ada;
    }

}
